package com.shinechain.service.user.impl;

import com.shinechain.botModel.ShineChainChatBot;
import com.shinechain.service.user.Proxy;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.telegram.telegrambots.ApiContext;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.generics.LongPollingBot;


public  class ProxyImpl implements Proxy {

    private static String BOT_NAME = "ShinechainBot";
    private static String BOT_TOKEN = "634777415:AAFmTHfxYspSOudhzhsqmfajH37VoUla-IU" /* your bot's token here */;

    private static String PROXY_HOST = "127.0.0.1" /* proxy host */;
    private static Integer PROXY_PORT = 1080/* proxy port */;
    private static String PROXY_USER = "lsq" /* proxy user */;
    private static String PROXY_PASSWORD = "lsq" /* proxy password */;

    private static ShineChainChatBot shineChainChatBot ;

    @Override
    public void init() {
        try {

            ApiContextInitializer.init();

            // Create the TelegramBotsApi object to register your bots
            TelegramBotsApi botsApi = new TelegramBotsApi();

            // Set up Http proxy
            DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
                    new AuthScope(PROXY_HOST, PROXY_PORT),
                    new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));

            HttpHost httpHost = new HttpHost(PROXY_HOST, PROXY_PORT);

            RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).setAuthenticationEnabled(true).build();
            botOptions.setRequestConfig(requestConfig);
            botOptions.setCredentialsProvider(credsProvider);
            botOptions.setHttpProxy(httpHost);

            shineChainChatBot = new ShineChainChatBot(botOptions);

            botsApi.registerBot((LongPollingBot) shineChainChatBot);
            //botsApi.registerBot((LongPollingBot) new InviteCheckBot(botOptions));
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
        //	SpringApplication.run(Springboot2MybatisDemoApplication.class, args);
    }

    public static  ShineChainChatBot getShineChainChatBot() {
        return shineChainChatBot;
    }
}
